package com.wxmit.ot.gen.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxmit.ot.base.core.OtBaseServiceImpl;
import com.wxmit.ot.base.core.PageDTO;
import com.wxmit.ot.gen.domain.GenGroup;
import com.wxmit.ot.gen.domain.GenGroupItem;
import com.wxmit.ot.gen.mapper.GenGroupMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * GenGroupService
 *
 * @author huangxiong
 * @version V1.0
 * @date 2023/2/22 10:44
 */
@Service
@RequiredArgsConstructor
public class GenGroupService extends OtBaseServiceImpl<GenGroupMapper, GenGroup> {

    private final GenGroupItemService groupItemService;

    public List<GenGroupItem> listFileById(Long id){
        return groupItemService.listByGroupId(id);
    }
    
    public List<GenGroup> listWithItem(GenGroup searchData) {

        return baseMapper.otSelectWithItem(searchData);
    }

    @Override
    public GenGroup getById(Serializable id) {

        return baseMapper.otGetById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean save(GenGroup entity) {
        boolean result =  super.save(entity);
        processItems(result,entity);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean updateById(GenGroup entity) {
        boolean result =  super.updateById(entity);
        processItems(result,entity);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean removeByIds(Collection<?> list) {
        boolean result =   super.removeByIds(list);
        if(result){
            groupItemService.removeBatchByGroupIds(list);
        }
        return result;
    }

    public List<GenGroup> listSelectOptions() {
        LambdaQueryWrapper<GenGroup> lambdaQuery = getLambdaQuery();

        lambdaQuery.select(GenGroup::getId,GenGroup::getName,GenGroup::getExtra);

        return baseMapper.selectList(lambdaQuery);
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void copy(Long id) {
        GenGroup genGroup = getOne(getLambdaQuery().eq(GenGroup::getId,id));
        Assert.isTrue(!genGroup.isNull(),"模版组id有误，请刷新后重试！");
        List<GenGroupItem> genGroupItems = groupItemService.listByGroupId(id);
        genGroup.setId(null);
        genGroup.setName("${genGroup.getName()}-复制");
        boolean save = save(genGroup);
        Assert.isTrue(save,"模版拷贝失败，请联系管理员！");
        genGroupItems.forEach(it->{
            it.setGroupId(genGroup.getId());
            it.setId(null);
        });
        groupItemService.saveBatch(genGroupItems);
    }

    private void processItems(boolean result, GenGroup entity){
        if(result && !CollectionUtil.isEmpty(entity.getItems())) {
            entity.getItems().forEach(it->{
                it.setGroupId(entity.getId());
                it.processPath();
            });
            // 查询删除元素
            List<Long> itemIds = entity.getItems().map(GenGroupItem::getId).toList();
            if(!CollectionUtil.isEmpty(itemIds)){
                List<Long> genGroupItemIds = groupItemService.listIdByGroupId(entity.getId());
                List<Long> needToDelIds = genGroupItemIds.stream().filter(it -> !itemIds.contains(it)).toList();
                if(!CollectionUtil.isEmpty(needToDelIds))groupItemService.removeBatchByIds(needToDelIds);
            }

            groupItemService.saveOrUpdateBatch(entity.getItems());
        }
    }
}
